package com.javasm.mb_store_api_final.giftsandgoods.gift.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.javasm.mb_store_api_final.giftsandgoods.gift.entity.GmGift2Goods;
import com.javasm.mb_store_api_final.giftsandgoods.gift.vo.Gifts2GoodsVo;
import com.javasm.mb_store_api_final.giftsandgoods.goods.entity.GgGoods;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 赠品管理-赠品转商品(GmGift2Goods)表数据库访问层
 *
 * @author makejava
 * @since 2024-08-01 11:25:47
 */
public interface GmGift2GoodsDao extends BaseMapper<GmGift2Goods> {

    @Select("<script>"+
            "SELECT g2g.id, gd.gname gift_name, gt.type_name gift_type,gd.market_price,g2g.count,g2g.apply_status\n" +
            "from gg_goods gd, gm_gift gf,gg_goods_type gt,gm_gift_2_goods g2g \n" +
            "WHERE gf.goods_id = gd.id and gt.id =gd.gtype \n" +
            "and g2g.gift_id=gf.id"
            + "<if test='giftName != null and giftName.trim().length() > 0'>"
            + "AND gd.gname LIKE CONCAT('%', #{giftName}, '%') "
            + "</if>"
            + "<if test='giftType != null'>"
            + "AND gt.type_name = #{giftType} "
            + "</if>"
            + "<if test='applyStatus != null'>"
            + "AND g2g.apply_status = #{applyStatus} "
            + "</if>"
            + "<if test='pageNum != null'>"
            + " LIMIT #{pageNum} ,#{pageSize} "
            + "</if>"
            + "</script>"
    )
    List<Gifts2GoodsVo> listGifts2GoodsVo( Gifts2GoodsVo g2gVo);

    @Select("SELECT count(*) FROM gm_gift_2_goods")
    long getTatol();

    @Select("SELECT  DISTINCT gt.type_name from gg_goods gd,gg_goods_type gt,gm_gift gf,gm_gift_2_goods g2g\n" +
            "WHERE gt.id =gd.gtype and gf.goods_id = gd.id and g2g.gift_id=gf.id")
    List<String> listgtype();

    @Select("SELECT id,gift_id,count,create_time,update_time,apply_user,apply_status,opinion FROM gm_gift_2_goods WHERE id=#{id}")
    GmGift2Goods getById(Integer id);

    @Update("<script>"+
            "UPDATE gm_gift_2_goods SET update_time=now() "
            + "<if test='count != null'>"
            + ",count = #{count}"
            + "</if>"
            +"<if test='opinion != null and opinion.trim().length() > 0'>"
            + ", opinion = #{opinion} "
            + "</if>"
            +"<if test='applyStatus != null and applyStatus.trim().length() > 0'>"
            + ", apply_status = #{applyStatus} "
            + "</if>"
            +"WHERE id=#{id}"
            + "</script>")
    Integer updateById1(GmGift2Goods gift2Goods);

    @Delete( "DELETE FROM gm_gift_2_goods WHERE id=#{id}")
    Integer delById(Integer id);

    @Insert( "INSERT INTO gm_gift_2_goods(gift_id,count,create_time,update_time,apply_user,apply_status) VALUES(#{giftId},#{count},now(),now(),#{applyUser},#{applyStatus})")
    boolean save(GmGift2Goods g2g);

    @Select("SELECT gg_goods.* FROM gg_goods ,gm_gift,gm_gift_2_goods g2g \n" +
            "WHERE gg_goods.id =gm_gift.goods_id  and gm_gift.id =g2g.gift_id and g2g.id=#{id}")
    GgGoods getDetailById(Integer id);

    @Update("UPDATE gm_gift\n" +
            "SET gift_num = gift_num - #{count}\n" +
            "WHERE id = (SELECT  gift_id FROM gm_gift_2_goods WHERE id =#{id})\n" +
            "AND gift_num>=#{count}")
    boolean updateGiftNum(@Param("id") Integer id,@Param("count") Integer count);
}

